package com.wty.emp.controller;

import com.wty.commons.result.Result;
import com.wty.commons.utils.ObjectToMapUtil;
import com.wty.emp.model.EmpJobWty;
import com.wty.emp.pojo.EmpWty;
import com.wty.emp.service.EmpServiceWty;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:wangtianyu
 * @Date：Created on 2020/4/20 0020 20:51
 */
@RestController
@RequestMapping("emps")
public class EmpControllerWty {

    /*测试*/
//    @ApiOperation(value = "测试", notes = "测试Swagger是否可用")
//    @ApiImplicitParam(name = "name", value = "测试的姓名", required = true, dataTypeClass = String.class)
//    @GetMapping("hello/{name}")
//    public Result hello(@PathVariable String name) {
//        EmpJobWty emp = new EmpJobWty();
//        emp.setEmpName(name);
//        return Result.OK(emp);
//    }

    @Autowired
    private EmpServiceWty empService;

    /*新增员工*/
    @ApiOperation(value = "新增员工", notes = "新增员工的基本信息")
    @ApiImplicitParam(name = "emp", value = "用户信息", required = true, dataType = "EmpWty", dataTypeClass = EmpWty.class)
    @PostMapping()
    public Result addEmp(@RequestBody EmpWty emp) throws Exception { // RequestBody 将数据写成form-data
        emp = empService.addEmp(emp);
        return Result.OK(emp);
    }

    /*修改员工信息*/
    @ApiOperation(value = "修改员工信息", notes = "修改员工基本信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "修改的员工Id", required = true, dataTypeClass = Long.class),
        @ApiImplicitParam(name = "emp", value = "修改的基本信息", required = true, dataType = "EmpWty", dataTypeClass = EmpWty.class)
    })
    @PutMapping("{id}")
    public Result updateEmp(@PathVariable Long id, @RequestBody EmpWty emp) throws Exception { // RequestBody 接收参数
        emp = empService.updateEmp(id, emp);
        return Result.OK(emp);
    }

    /*搜索*/
    @ApiOperation(value = "搜索",notes = "姓名或地址搜索")
    @ApiImplicitParam(name = "emp", value = "搜索的条件", required = true, dataType = "EmpWty", dataTypeClass = EmpWty.class)
    @PostMapping("search")
    public Result searchByExample(@RequestBody EmpWty emp)throws Exception{
        List<EmpJobWty> empJobs = empService.searchByExample(ObjectToMapUtil.convertToMap(emp));
        return Result.OK(empJobs);
    }

    /*遍历*/
    @ApiOperation(value = "遍历",notes = "遍历所有数据")
    @GetMapping
    public Result selectList()throws Exception{
        List<EmpWty> emps = empService.selectList();
        return Result.OK(emps);
    }

}
